const _PREFAB = "prefabs/ui/Waiting";   //prefab动态加载路径

let _instanceNode;
let instance;
let Waiting = cc.Class({
  extends: cc.Component,
  
  onLoad() {
    this.node.active = false;
    cc.CEventManager.instance.registerEvent(EVENTS.SCENE_LOADED, this, 0, "onSceneLoaded");
  },
  onDestroy() {
    cc.CEventManager.instance.releaseAllEvents(this);
  },
  onSceneLoaded() {
    this.node.zIndex = cc.macro.MAX_ZINDEX;
  }
});

Waiting.init = function (callback) {
  if (!instance) {
    cc.loader.loadRes(_PREFAB, cc.Prefab, function (err, prefab) {
      _instanceNode = cc.instantiate(prefab);
      _instanceNode.parent = cc.director.getScene();
      instance = _instanceNode.getComponent(Waiting);
      cc.game.addPersistRootNode(_instanceNode);
      if (callback) {
        callback();
      }
    });
  }
};

function _doShow() {
  instance.node.zIndex = cc.macro.MAX_ZINDEX;
  instance.node.active = true;
}

function _doHide() {
  instance.node.active = false;
}

Waiting.show = function () {
  if (instance) {
    _doShow();
  } else {
    Waiting.init(() => {
      _doShow();
    })
  }
};

Waiting.hide = function () {
  if (instance) {
    _doHide();
  }
  
};

module.exports = Waiting;